package com.wf.smartcomm.server.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wf.smartcomm.server.bean.User;

public class LoginFilter extends HttpServlet implements Filter {

	@Override
	public void doFilter(ServletRequest sRequest, ServletResponse sResponse,
			FilterChain chain) throws IOException, ServletException {	
		HttpServletRequest request = (HttpServletRequest) sRequest;        
        HttpServletResponse response = (HttpServletResponse) sResponse;
		HttpSession session = request.getSession(false);
		String url=request.getServletPath();
		
		if(url.endsWith("index.jsp")){
			chain.doFilter(sRequest, sResponse);
			return;
		}
		
		if (session == null) {
			response.sendRedirect(request.getContextPath()+"/jsp/index.jsp");
			return;
		}
		
		User user = (User) session.getAttribute("loginUser");
		if (user == null) {
			response.sendRedirect(request.getContextPath()+"/jsp/index.jsp");
		}else {
			chain.doFilter(sRequest, sResponse);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
